package edu.njust.cma.mapper;

import edu.njust.cma.pojo.Submission;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;
import java.util.concurrent.CountDownLatch;

@Mapper
public interface SubmissionMapper {

    // 查询是否未审核的提交 status = 0
    @Select("select count(*) from submissions where task_id=#{taskId} and review_status=0")
    Integer getUncheckSubmissionCountByTaskId(Integer taskId);

    // 更新审核状态
    @Update("update submissions set review_status=#{status} where id=#{id}")
    void updateStatusById(Integer id, int status);

    //更新反馈
    @Update("update submissions set feedback=#{feedback} where id=#{id}")
    void updateFeedbackById(Integer id, String feedback);

    @Select("select id from submissions where subtask_id=#{subtaskId} and type=#{type} and review_status=0")
    int getSubmissionIdBySubTaskId(Integer subtaskId, Integer type);

    @Select("select id from submissions where task_id=#{taskId} and type=#{type} and review_status=0")
    int  getSubmissionIdByTaskId(Integer taskId, Integer type);

    @Select("select * from submissions where task_id=#{taskId} and type=#{type} and review_status=0")
    int getStatusBySubTaskId(int id);

    @Select("select feedback from submissions where id = #{id}")
    String getFeedbackBySubtaskId(int id);


    void updateStatus(int taskId, int id, int drop);

    @Update("update submissions set submission_time=#{submissionTime} " +
            "where task_id=#{taskId} and type=#{type} and subtask_id=#{subtaskId}")
    void updateForCommit(int taskId, int type, String submissionTime, int subtaskId);



    // id最后最大的一个
    @Select("select * from submissions where type=#{type} and subtask_id=#{subtaskId} order by id desc limit 1")
    Submission getSubmissionLastBySubtaskIdAndType(int subtaskId, int type);

    @Insert("insert into submissions (task_id, type, subtask_id,  submission_time, review_status) " +
            "values (#{taskId}, #{type}, #{subtaskId}, #{submissionTime}, #{reviewStatus})")
    void create(Submission submission);

    @Update("update submissions set review_status=#{status} where task_id=#{taskId} and type=#{type} and review_status = 0")
    void updateStatusWaitByTaskIdAndType(int taskId, int type, int status);

    @Select("select * from submissions where task_id=#{taskId} and type=#{type} and review_status = 0")
    Submission getUncheckSubmissionByTaskIdAndType(int taskId,int type);
}
